package com.sisyphus.spinach.ui.configs;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

/**
 * activity 配置
 */
public class ActivityConfigRegister implements Application.ActivityLifecycleCallbacks {

    private final IGlobalActivityConfigProvider mProvider;


    public ActivityConfigRegister(IGlobalActivityConfigProvider provider) {

        mProvider = provider;
    }


    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        Log.i("ACTIVITY_ORDER", "ActivityConfigRegister >> onActivityCreated");

        // 注册fragment配置
        if (activity instanceof AppCompatActivity) {
            FragmentManager supportFragmentManager = ((AppCompatActivity) activity).getSupportFragmentManager();

            List<Fragment> fragments = supportFragmentManager.getFragments();

            if (!fragments.isEmpty()) {
                supportFragmentManager.registerFragmentLifecycleCallbacks(new SupportFragmentConfigRegister(), false);
            }
        }

        // 全局activity配置
        if (!(activity instanceof IConfigIgnore)) {
            mProvider.initActivity(activity);
        }

        // 单一activity配置
        if (activity instanceof IActivityConfigProvider) {
            activity.setContentView(((IActivityConfigProvider) activity).getLayoutResID());
        }

        // 列表模板
//        if (activity instanceof IListTemplateProvider) {
//            ((IListTemplateProvider<?>) activity).provider().created(activity, null);
//        }
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

}
